package com.yizhuoyan.his.webapi.controller.admin;

import com.yizhuoyan.his.ao.FinExpenseCategoryAo;
import com.yizhuoyan.his.dto.JSONResponse;
import com.yizhuoyan.his.entity.FinExpenseCategoryEntity;
import com.yizhuoyan.his.service.admin.FinanceExpenseCategoryManageService;
import com.yizhuoyan.his.webapi.controller.admin.vo.FinExpenseCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class FinanceExpenseCategoryManageController  {

    @Autowired
    FinanceExpenseCategoryManageService expenseCategoryManageService;

    @PostMapping("/api/expense-category/")
    public ResponseEntity<JSONResponse> addExpenseCategory(FinExpenseCategoryAo ao)throws Exception{
        FinExpenseCategoryEntity e = expenseCategoryManageService.addExpenseCategory(ao);
        return ResponseEntity.ok().build();
    }
    @DeleteMapping("/api/expense-category/{id}")
    public ResponseEntity<Void> deleteExpenseCategory(@PathVariable Long id)throws Exception{
        expenseCategoryManageService.removeExpenseCategory(id);
        return ResponseEntity.ok().build();
    }
    @PutMapping("/api/expense-category/{id}")
    public ResponseEntity<Void> modifyExpenseCategory(@PathVariable Long id, FinExpenseCategoryAo ao)throws Exception{
        expenseCategoryManageService.modifyExpenseCategory(id,ao);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/api/expense-category/{id}")
    public ResponseEntity<FinExpenseCategoryVo> loadExpenseCategory(@PathVariable Long id)throws Exception{
        FinExpenseCategoryEntity entity = expenseCategoryManageService.queryExpenseCategory(id);
        return ResponseEntity.ok(FinExpenseCategoryVo.of(entity));
    }

    @GetMapping("/api/expense-category")
    public ResponseEntity<List<FinExpenseCategoryVo>> loadExpenseCategory(String key)throws Exception{
        List<FinExpenseCategoryEntity> result = expenseCategoryManageService.queryExpenseCategoriesByKey(key);
        return ResponseEntity.ok(
                result.stream().
                        map(FinExpenseCategoryVo::of)
                        .collect(Collectors.toList()));
    }

    @GetMapping(path = "/api/expense-category/validate",params = "code")
    public ResponseEntity<JSONResponse> validateExpenseCategoryCode(String code)throws Exception{
        expenseCategoryManageService.validateExpenseCategoryCodeValid(code);
        return ResponseEntity.ok(JSONResponse.ok());
    }
}
